<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>出勤管理</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<!--列表显示-->
<table id="attendanceTable" lay-filter="attendanceFilter"></table>

<script type="text/html" id="barRowEdit">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!--工具栏模板：-->
<script type="text/html" id="toolbarHead">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>
    </div>
</script>

<form id="attendanceAddForm" class="layui-form" style="display:none"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-form-item">
        <label class="layui-form-label">用户编号</label>
        <div class="layui-input-block">
            <input type="text" name="userId" id="nameTableSelect" aria-required="true"  placeholder="请输入" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">出勤时间</label>
        <div class="layui-input-inline">
            <input type="text" name="attendanceTime"  id="attendanceTime" placeholder="YYYY-MM-DD HH:MM:ss" lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>
<!--    <div class="layui-form-item">-->
<!--        <label class="layui-form-label">部门状态</label>-->
<!--        <div class="layui-input-block">-->
<!--            <input type="radio" name="status" value="0" title="启动" checked>-->
<!--            <input type="radio" name="status" value="1" title="禁用">-->
<!--        </div>-->
<!--    </div>-->

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="attendanceAddFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
    <!-- 更多表单结构排版请移步文档左侧【页面元素-表单】一项阅览 -->
</form>


<form id="attendanceUpdateForm" class="layui-form" lay-filter="attendanceUpdateFilter" style="display:none"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <input type="hidden" name="attendanceId">
    <div class="layui-form-item">
        <label class="layui-form-label">用户编号</label>
        <div class="layui-input-block">
            <input type="text" name="userId" id="nameTableSelect1" placeholder="请输入" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div><div class="layui-form-item">
        <label class="layui-form-label">出勤时间</label>
        <div class="layui-input-block">
            <input type="text" name="attendanceTime"  id="attendanceTime1" placeholder="YYYY-MM-DD HH:MM:ss" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>



<!--    <div class="layui-form-item">-->
<!--        <label class="layui-form-label">部门状态</label>-->
<!--        <div class="layui-input-block">-->
<!--            <input type="radio" name="status" value="0" title="启动" checked>-->
<!--            <input type="radio" name="status" value="1" title="禁用">-->
<!--        </div>-->
<!--    </div>-->

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="attendanceUpdateFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="/layui/layui.js"></script>
<script>
    //自定义layui组件的目录
    layui.config({
        base: '/layui/lay/modules/'
    }).extend({
        //设定组件别名
        tableSelect: 'tableSelect',
    });
    layui.use(['layer','table', 'laydate','jquery','form', 'tableSelect'], function(){
        let table = layui.table;
        let layer = layui.layer;
        let $ = layui.jquery;
        let form = layui.form;
        let laydate = layui.laydate;
        let tableSelect = layui.tableSelect
        //第一个实例
        laydate.render({
            elem: '#attendanceTime1'
            ,type: 'datetime'
        });
        laydate.render({
            elem: '#attendanceTime'
            ,type: 'datetime'
        });
        tableSelect.render({
            elem: '#nameTableSelect',	//定义输入框input对象 必填
            checkedKey: 'userId', //表格的唯一建值，非常重要，影响到选中状态 必填
            searchKey: 'userId',	//搜索输入框的name值 默认keyword
            searchPlaceholder: '编号搜索',	//搜索输入框的提示文字 默认关键词搜索
            table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                url: '/user/queryAll',
                cols: [
                    [
                        {type: 'radio'}
                        , {field: 'userId', title: '编号', width: 80, sort: true}
                        , {field: 'loginName', title: '登录名', width: 100}
                        , {field: 'userName', title: '真实姓名', width: 120}
                    ]
                ]
            },
            done: function (elem, data) {
                //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
                //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
                elem.val(data.data[0].userId)
            }
        })
        tableSelect.render({
            elem: '#nameTableSelect1',	//定义输入框input对象 必填
            checkedKey: 'userId', //表格的唯一建值，非常重要，影响到选中状态 必填
            searchKey: 'userId',	//搜索输入框的name值 默认keyword
            searchPlaceholder: '编号搜索',	//搜索输入框的提示文字 默认关键词搜索
            table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                url: '/user/queryAll',
                cols: [
                    [
                        {type: 'radio'}
                        , {field: 'userId', title: '编号', width: 80, sort: true}
                        , {field: 'loginName', title: '登录名', width: 100}
                        , {field: 'userName', title: '真实姓名', width: 120}
                    ]
                ]
            },
            done: function (elem, data) {
                //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
                //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
                elem.val(data.data[0].userId)
            }
        })
        let attendanceTable = table.render({
            elem: '#attendanceTable'
            ,toolbar: '#toolbarHead'
            ,url: '/attendance/queryAll/' //数据接口
            ,page: true //开启分页
            ,cellMinWidth:80
            ,cols: [
                [ //表头
                 {type: 'checkbox',fixed: 'left'}
                ,{field: 'attendanceId', title: 'ID',  sort: true, fixed: 'left'}
                ,{field: 'userId', title: '用户编号', sort:true}
                ,{field: 'attendanceTime', title: '出勤时间', sort: true}
                // ,{field: 'updateBy', title: '更新人',  sort: true}
                // ,{field: 'updateTime', title: '更新时间'}
                ,{fixed: 'right',  align:'center', toolbar: '#barRowEdit'}
            ]
            ]
        });

        //监听工具条
        table.on('tool(attendanceFilter)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        url:"/attendance/deleteById",
                        type:"DELETE",
                        data:{attendanceId:data.attendanceId},
                        dataType:"json",
                        success:function(resp){
                            if(resp.code==0){
                                layer.msg(resp.msg);
                                attendanceTable.reload();
                            }
                        },
                        error:function(e){
                            layer.msg("请求失败");
                        }
                    });
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                // layer.alert('编辑行：<br>'+ JSON.stringify(data))
                form.val("attendanceUpdateFilter",data);
                layer.open({
                    type: 1,
                    area:['500px','500px'],
                    content: $('#attendanceUpdateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
            }
        });

        //头部工具栏监听事件
        table.on('toolbar(attendanceFilter)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            var checkData = checkStatus.data;
            switch(obj.event){
                case 'add':
                    // layer.msg('添加');
                    // $("#attendanceAddForm").reset();
                    document.getElementById("attendanceAddForm").reset();
                    layer.open({
                        type: 1,
                        area:['500px','500px'],
                        content: $('#attendanceAddForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
                case 'delete':
                    // layer.msg('删除');
                    // layer.msg(JSON.stringify(checkData));
                    if(checkData.length==0){
                        layer.msg('至少选择一行');
                        return;
                    }
                    layer.confirm('真的删除吗',function(index){
                        var attendanceIds = "";
                        for(var i = 0; i < checkData.length; i++){
                            attendanceIds+=checkData[i].attendanceId+",";
                        }
                        attendanceIds=attendanceIds.substr(0,attendanceIds.length-1);
                        layer.msg(attendanceIds);
                        $.ajax({
                            url:"/attendance/deleteByIds",
                            type:"DELETE",
                            data:{attendanceIds:attendanceIds},
                            dataType:"json",
                            success:function(resp){
                                if(resp.code==0){
                                    layer.msg(resp.msg);
                                    attendanceTable.reload();
                                }
                            },
                            error:function(e){
                                layer.msg("请求失败");
                            }
                        });
                    });
                    break;
                case 'update':
                    // layer.msg('编辑');
                    if(checkData.length!=1){
                        layer.msg('只能选中一行');
                        return;
                    }
                    form.val("attendanceUpdateFilter",checkData[0]);
                    layer.open({
                        type: 1,
                        area:['500px','500px'],
                        content: $('#attendanceUpdateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
            };
        });

        //监听提交
        form.on('submit(attendanceAddFilter)', function(data){
            layer.msg(JSON.stringify(data.field));
            $.ajax({
                url:"/attendance/insert",
                type:"POST",
                data:data.field,
                dataType:"json",
                success:function(resp){
                    if(resp.code==0){
                        layer.msg(resp.msg);
                        layer.closeAll('page');
                        attendanceTable.reload();
                    }
                },
                error:function(e){
                    layer.msg("请求失败");
                }
            });
            return false;
        });

        form.on('submit(attendanceUpdateFilter)', function(data){
            layer.msg(JSON.stringify(data.field));
            $.ajax({
                url:"/attendance/update",
                type:"PUT",
                data:data.field,
                dataType:"json",
                success:function(resp){
                    if(resp.code==0){
                        layer.msg(resp.msg);
                        layer.closeAll('page');
                        attendanceTable.reload();
                    }
                },
                error:function(e){
                    layer.msg("请求失败");
                }
            });
            return false;
        });
    });
</script>
</body>
</html>